home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 6 / The Arsenal Files 6 (Arsenal Computer).ISO / os2bbs / fbrow112.zip / XGETCH.MH < prev   
Text File  |  1995-10-13  |  6KB  |  215 lines

  1. //---------------------------cut-here--8<-------------------------------------
  2. // 
  3. //
  4. //  xgetch.mh
  5. //  eXtended GETCH()       and extended input()   (xinput())
  6. //  by Golden Spud
  7. //
  8. //  handles ANSI/WordStar cursor keys from remote as well as local
  9. //
  10. //  known bugs:
  11. //      wont accept all keys.
  12. //      INS (^[On) needs to be added to ANSI
  13. //
  14.  
  15. #ifndef __XGETCH_MH
  16. #define __XGETCH_MH
  17.  
  18. #define X_UP        72
  19. #define X_DOWN      80
  20. #define X_LEFT      75
  21. #define X_RIGHT     77
  22. #define X_HOME      71
  23. #define X_END       79
  24. #define X_INSERT    82
  25. #define X_DELETE    83
  26. #define X_PAGEUP    73
  27. #define X_PAGEDOWN  81
  28.  
  29. #define X_INVALID   255
  30. #define X_EMPTY     0
  31.  
  32. // this is the number of tries that xgetch() will wait for the rest of
  33. // an ANSI escape sequence before assuming that the escape key was pressed
  34. // instead of an arrow key.
  35. #define XGETCH_ANSI_TRIES       500
  36.  
  37. char: _xgetchinput;
  38.  
  39. char xgetch()
  40. {
  41.     char: tempch;
  42.  
  43.     // if there's a character waiting, let's return it without wasting
  44.     // any time.
  45.     if (_xgetchinput <> X_EMPTY) {
  46.         tempch := _xgetchinput;
  47.         _xgetchinput := X_EMPTY;
  48.         return tempch;
  49.     }
  50.  
  51.     tempch := getch();
  52.  
  53.     // is it an extended character? if so, store it and return 0.
  54.     if (tempch = 0) {
  55.         _xgetchinput := getch();
  56.         return tempch;
  57.     }
  58.  
  59.     // -------------------- WORDSTAR DIAMOND CONTROLS ------------------
  60.     if (tempch = 23) {                 // ctrl-w = home
  61.         _xgetchinput := X_HOME;
  62.         return 0;
  63.     }
  64.     if (tempch = 16) {                 // ctrl-p = end
  65.         _xgetchinput := X_END;
  66.         return 0;
  67.     }
  68.     if (tempch = 18) {                 // ctrl-r = pgup
  69.         _xgetchinput := X_PAGEUP;
  70.         return 0;
  71.     }
  72.     if (tempch = 3) {                  // ctrl-c = pgdn
  73.         _xgetchinput := X_PAGEDOWN;
  74.         return 0;
  75.     }
  76.     if (tempch = 5) {                  // ctrl-e = up
  77.         _xgetchinput := X_UP;
  78.         return 0;
  79.     }
  80.     if (tempch = 24) {                 // ctrl-x = down
  81.         _xgetchinput := X_DOWN;
  82.         return 0;
  83.     }
  84.     if (tempch = 19) {                 // ctrl-s = left
  85.         _xgetchinput := X_LEFT;
  86.         return 0;
  87.     }
  88.     if (tempch = 4) {                  // ctrl-d = right
  89.         _xgetchinput := X_RIGHT;
  90.         return 0;
  91.     }
  92.     if (tempch = 22) {                 // ctrl-v = insert
  93.         _xgetchinput := X_INSERT;
  94.         return 0;
  95.     }
  96.     if (tempch = 7) {                  // ctrl-g = del
  97.         _xgetchinput := X_DELETE;
  98.         return 0;
  99.     }
  100.  
  101.     // -------------------- ANSI ESCAPE CONTROLS ------------------------
  102.     if (tempch = 27) {                 // escape means it's an ANSI code
  103.         int: tries;
  104.         char: tempch2;
  105.  
  106.         // here we need to test to see if the [ character comes through.
  107.         tries := 0;
  108.         while (tries < XGETCH_ANSI_TRIES)
  109.             if (kbhit() = FALSE)
  110.                 tries := tries + 1;
  111.             else
  112.                 goto xgetch_breakout1;
  113.         xgetch_breakout1:
  114.  
  115.         // if no key has been hit yet, assume the user actually pressed
  116.         // ESC and return that.
  117.         if (kbhit() = FALSE)
  118.             return 27;
  119.  
  120.         tempch2 := getch();
  121.  
  122.         // if the input character was not '[', then assume the escape key
  123.         // was hit, then some other character.
  124.         if (tempch2 <> '[') {
  125.             _xgetchinput := tempch2;
  126.             return 27;
  127.         }
  128.  
  129.         // now we need to test for the next character. if it isn't in the
  130.         // list of valid characters, we will return 0 with X_INVALID as the     
  131.   //  extended
  132.         // key, meaning an invalid key.
  133.         // if it doesn't come in, we'll assume the user pressed escape, then
  134.         // [ and we'll return both of those.
  135.         tries := 0;
  136.         while (tries < XGETCH_ANSI_TRIES)
  137.             if (kbhit() = FALSE)
  138.                 tries := tries + 1;
  139.             else
  140.                 goto xgetch_breakout2;
  141.         xgetch_breakout2:
  142.  
  143.         if (kbhit() = FALSE) {
  144.             _xgetchinput := '[';
  145.             return 27;
  146.         }
  147.  
  148.         tempch2 := getch();
  149.  
  150.         if (tempch2 = 'H') {               // home
  151.             _xgetchinput := X_HOME;
  152.             return 0;
  153.         }
  154.         if (tempch2 = 'K') {               // end
  155.             _xgetchinput := X_END;
  156.             return 0;
  157.         }
  158.         if (tempch2 = 'A') {               // up
  159.             _xgetchinput := X_UP;
  160.             return 0;
  161.         }
  162.         if (tempch2 = 'B') {               // down
  163.             _xgetchinput := X_DOWN;
  164.             return 0;
  165.         }
  166.         if (tempch2 = 'D') {               // left
  167.             _xgetchinput := X_LEFT;
  168.             return 0;
  169.         }
  170.         if (tempch2 = 'C') {               // right
  171.             _xgetchinput := X_RIGHT;
  172.             return 0;
  173.         }
  174.         if (tempch2 = 'O') {               // pgup or pgdn -- further testing
  175.             char: tempch3;
  176.  
  177.             tries := 0;
  178.             while (tries < XGETCH_ANSI_TRIES)
  179.                 if (kbhit() = FALSE)
  180.                     tries := tries + 1;
  181.                 else
  182.                     goto xgetch_breakout3;
  183.             xgetch_breakout3:
  184.  
  185.             if (kbhit() = FALSE) {
  186.                 _xgetchinput := X_INVALID;
  187.                 return 0;
  188.             }
  189.  
  190.             if (tempch3 = 'r') {       // pgup
  191.                 _xgetchinput := X_PAGEUP;
  192.                 return 0;
  193.             }
  194.             if (tempch3 = 'q') {       // pgdn
  195.                 _xgetchinput := X_PAGEDOWN;
  196.                 return 0;
  197.             }
  198.             // invalid ^[[O sequence
  199.             _xgetchinput := X_INVALID;
  200.             return 0;
  201.         }
  202.  
  203.         // invalid ^[[ sequence
  204.         _xgetchinput := X_INVALID;
  205.         return 0;
  206.     }
  207.  
  208.     // it was a normal key and should be returned as such.
  209.     return tempch;
  210. }
  211.  
  212. #endif
  213.  
  214.  
  215.